package com.example.webtest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "data";
    private ListView newsList=null;
    private int icon=R.mipmap.ic_launcher;
    private ImageView myImage=null;
    private TextView newsText;
    String json;
    List<HashMap<String, Object>> jsonMap=new ArrayList<HashMap<String, Object>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webThread.start();
        try {
            webThread.join();
            getJson();
            initView();
            initListener();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void initListener() {
        newsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Log.e("点击：",jsonMap.get(i).get("title").toString());
                Intent intent=new Intent(MainActivity.this,NewsActivity.class);
                final SerializableMap myMap=new SerializableMap();
                myMap.setMap(jsonMap.get(i));//将map数据添加到封装的myMap中
                Bundle bundle=new Bundle();
                bundle.putSerializable("map", myMap);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        newsList=findViewById(R.id.listview);
        MyAdapter myAdapter=new MyAdapter();
        newsList.setAdapter(myAdapter);
    }

    private void getJson() {
        try {
//            Log.e(TAG,json);
            JSONObject rootObject=new JSONObject(json);
            JSONArray dataArray=rootObject.getJSONArray("data");
            for(int i=0;i<dataArray.length();i++)
            {
                JSONObject newsObject = dataArray.getJSONObject(i);
                String imageAll=newsObject.getString("image");
                String imageTmp=imageAll.substring(1,imageAll.length()-1);
//                String[] imageArray=imageTmp.split(", ");
                String video= newsObject.getString("video");
                String title= newsObject.getString("title");
                String content=newsObject.getString("content");
                String organizations= newsObject.getString("organizations");
//                Log.e(TAG,title);
                HashMap<String, Object> map = new HashMap<>();
                map.put("image",imageTmp);
                map.put("video", video);
                map.put("title", title);
                map.put("content",content);
                map.put("organization", video);
                jsonMap.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    Thread webThread = new Thread(new Runnable() {
        @Override
        public void run() {
            getWeb();
        }
    });

    private void getWeb() {
        String path = "https://api2.newsminer.net/svc/news/queryNewsList?size=15&startDate=2021-08-25&endDate=2021-08-30&words=&categories=科技";
        try {
            json = HtmlService.getHtml(path);
            System.out.println(json.length());
//            Log.e(TAG, "html : " + json);
//            System.out.println(html);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return jsonMap.size();
        }

        @Override
        public Object getItem(int i) {
            return jsonMap.get(i).get("title");
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View itemView=View.inflate(MainActivity.this,R.layout.itemlayout,null);
            newsText=itemView.findViewById(R.id.name);
            myImage=itemView.findViewById(R.id.picture);
            newsText.setText(jsonMap.get(i).get("title").toString());
            String imageAll=jsonMap.get(i).get("image").toString();
            String[] imageArray=imageAll.split(", ");
//            for(int j=0;j<imageArray.length;j++)
//            {
//                if(j>=1)
//                {
//                    Log.e("image:", String.valueOf(imageArray[j].charAt(0)));
//                }
//            }
            Glide.with(MainActivity.this).load(imageArray[0]).into(myImage);
            newsText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(MainActivity.this,NewsActivity.class);
                    final SerializableMap myMap=new SerializableMap();
                    myMap.setMap(jsonMap.get(i));//将map数据添加到封装的myMap中
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("map", myMap);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
//            myImage.setBackgroundResource(icon);
            return itemView;
        }
    }
}
